package com.istudy.servlet;

import com.istudy.entity.User;
import com.istudy.entity.Watch;
import com.istudy.service.interfaces.WatchService;
import com.istudy.service.imp.WatchServiceImpl;
import com.istudy.utils.ReturnResult;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 播放记录控制器
 *
 * @author draw_heart
 * @create 2020-08-26 16:58
 */
@WebServlet(urlPatterns = {"/Watch"},name = "watch")
public class WatchServlet extends AbstractServlet {
    public static WatchService watchService=new WatchServiceImpl();
    @Override
    public Class getServletClass() {
        return WatchServlet.class;
    }
    /**
     * 新增评论控制器
     * @param request
     * @param response
     * @return
     */
    public void insertWatch(HttpServletRequest request, HttpServletResponse response) {
        //实体类映射
        User user=(User)request.getAttribute("user");
        Watch watch=new Watch();
       watch.setUserId(user.getUserId());
        watch.setVideoId(Integer.parseInt(request.getParameter("videoid")));
        watch.setWatchTime(new Date().toString());
        watchService.insertWatch(watch);
    }
    /**
     * 播放记录控制器
     * @return
     */
    public String getWatchlist(HttpServletRequest request, HttpServletResponse response){
        if(request.getSession().getAttribute("user")!=null){
            int num=1;
            if(request.getParameter("currentPage")!=null) {
                num = Integer.parseInt(request.getParameter("currentPage"));
            }
            List<Watch> watchList = watchService.getWatch(request,num);
            request.setAttribute("watchList",watchList);
            return  "mywatch";
        }else {
            return  "login";
        }
    }
    /**
     * 删除记录控制器
     * @return
     */
    public ReturnResult delWatch(HttpServletRequest request, HttpServletResponse response){
       boolean i = watchService.delWatch(request);
        ReturnResult returnResult = new ReturnResult();
       if(i){
            returnResult.setStatus(1);
            returnResult.setMessage("删除成功");
        } else  {

            returnResult.setStatus(-1);
            returnResult.setMessage("删除失败");
        }
        return returnResult;

    }
}
